/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javafx179labs.nez.hr.dao.file;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;


/**
 *
 * @author civil
 */
class DataWriterHelper {
    public DataWriterHelper(String pathToFile, List<String> lines) {
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(pathToFile));
            Iterator<String> iter = lines.iterator();
            while (iter.hasNext()) {
                bufferedWriter.write(iter.next());
            }
        } catch (Exception e) {
            System.err.println("Error while writing file[" + pathToFile + "]." + e.getMessage());
            e.printStackTrace();
        } finally {
            closeWriter(bufferedWriter);
        }
    }

    private void closeWriter(Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                System.err.println("Error while closing writer." + e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
